home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / include / IUnknown.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-25  |  3.1 KB  |  119 lines

  1.  
  2. #ifndef __IUNKNOWN_H_
  3. #define __IUNKNOWN_H_
  4. /*
  5. Peon - Win32 Games Programming Library
  6. Copyright (C) 2002-2005 Erik Yuzwa
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public
  10. License as published by the Free Software Foundation; either
  11. version 2 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Library General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Library General Public
  19. License along with this library; if not, write to the Free
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. Erik Yuzwa
  23. peon AT wazooinc DOT com
  24. */
  25.  
  26. #include "Peonstdafx.h"
  27.  
  28. namespace peon
  29. {
  30.     /**
  31.     * This object represents a base level object that we can derive other
  32.     * objects from in the Peon library. It mainly works around a primitive
  33.     * and crude form of reference counting. As you copy interfaces around
  34.     * the engine, you increase the reference count of the object. As you
  35.     * delete/free them, you decrease the reference count. If the engine
  36.     * exits with some reference counts still above 0, then you know you're
  37.     * not freeing something properly.
  38.     *
  39.     * For those not happy with this implementation, you might want to take
  40.     * a look at the smart pointer object which comes with some of the
  41.     * boost libraries. http://www.boost.org. It's a handy collection of
  42.     * some C++ objects.
  43.     */
  44.     class PEONMAIN_API IUnknown
  45.     {
  46.     protected:
  47.         /** the "id" of our object...can be anything we want */
  48.         int        m_id;
  49.  
  50.         /** the internal reference count of this object */
  51.         int        m_refCount;
  52.  
  53.     public:
  54.         /**
  55.         * Constructor
  56.         */
  57.         IUnknown() : m_refCount(1), m_id(0)
  58.         {
  59.         }
  60.  
  61.         /**
  62.         * Destructor
  63.         */
  64.         virtual ~IUnknown()
  65.         {
  66.         
  67.         }
  68.  
  69.         /**
  70.         * This method just returns our id "tag" of the object.
  71.         * @return int - our object's id
  72.         */
  73.         int getID(){ return m_id; }
  74.  
  75.         /**
  76.         * This method just sets our id "tag" of the object.
  77.         * @param new_id - the new id we want to set our object to
  78.         */
  79.         void setID( int new_id ){ m_id = new_id; }
  80.  
  81.         /**
  82.         * This method increases our object's reference count. 
  83.         * 
  84.         * A simple example is when we pass around our SceneRenderer
  85.         * interface around through the engine. If another object
  86.         * uses an internal handle to it, then we've just copied
  87.         * a reference to the SceneRenderer!
  88.         *
  89.         * Instead just add a ref count when you assign it to 
  90.         * another object
  91.         *
  92.         */
  93.         void addRefCount() { ++m_refCount; }
  94.  
  95.         /**
  96.         * This method decreases our object's reference count
  97.         * @return bool - have we cleaned up every instance of this object?
  98.         */
  99.         bool dropRefCount()
  100.         {
  101.             //decrement the reference counter variable. If we drop
  102.             //below zero, then we can automatically clean this up
  103.             //with a delete call.
  104.             //
  105.             --m_refCount;
  106.             if (!m_refCount)
  107.             {
  108.                 delete this;
  109.                 return true;
  110.             }
  111.  
  112.             return false;
  113.         }
  114.  
  115.     };
  116. }
  117.  
  118. #endif
  119.